
import type { RouteRecordRaw } from "vue-router"

import router from '../router/index'

type IRoute = RouteRecordRaw & { key?: string };
//定义全部路由,admin下的子一级

const allRoutes: IRoute[] = [
    // wkw
    {
        key: "0-0-0",
        name: "dengji",
        path: "dengji",
        component: () => import("../views/wkw/DengJi/dengji.vue")
    },
    {
        key: "0-0-1",
        name: "classmange",
        path: "classmange",
        component: () => import("../views/wkw/ClassMange/classmange.vue")
    }
    ,
    {
        key: "0-0-2",
        name: "zhanghao",
        path: "zhanghao",
        component: () => import("../views/wkw/ZhangHao/zhanghao.vue")
    },
    // lxc
    {
        key: "1-0-0",
        name: 'flower',
        path: 'flower',
        component: () => import("../views/lxc/component/flower.vue")
    },
    {
        key: "1-0-1",
        name: 'blackwhite',
        path: 'blackwhite',
        component: () => import("../views/lxc/component/blackwhite.vue")
    },
    {
        key: "1-0-2",
        name: 'black',
        path: 'black',
        component: () => import("../views/lxc/component/black.vue")
    },
    // dy
    {
        key: "2-0-0",
        name: "kaoqin",
        path: "kaoqin",
        component: () => import("../views/dy/kao.vue")
    },
    {
        key: "2-0-1",
        name: "guize",
        path: "guize",
        component: () => import("../views/dy/rule.vue")
    },
    {
        key: "2-0-2",
        name: "jilu",
        path: "jilu",
        component: () => import("../views/dy/record.vue")
    },
    // hgq
    {
        key: "3-0-0",
        name: "pmanage",
        path: "pmanage",
        component: () => import("../views/hgq/project.vue"),

    },
    {
        key: "3-0-1",
        name: "emanage",
        path: "emanage",
        component: () => import("../views/hgq/equipment.vue")
    },
    //wxb
    {
        key: "4-0-0",
        name: "zhanghao1",
        path: "zhanghao1",
        component: () => import("../views/wxb/zhanghao/zhanghao1.vue")
    },
    {
        key: "4-0-1",
        name: "quanxian",
        path: "quanxian",
        component: () => import("../views/wxb/quanxian/quanxian.vue")
    },
    {
        key: "4-0-2",
        name: "zidian",
        path: "zidian",
        component: () => import("../views/wxb/zidian/zidian.vue")
    },
]


export default function getRoutes() {
    //1.获取当前用户的权限
    let checkedkeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    console.log(grants);

    if (grants) {
        checkedkeys = JSON.parse(grants)
    }
    if (checkedkeys.length === 0) {
        checkedkeys = ['0-0-0', '0-0-1', '0-0-2', '1-0-0', '1-0-1', '1-0-2', '2-0-0', '2-0-1', '2-0-2', '2-0-3', '3-0-0', '3-0-1', '4-0-0', '4-0-1', '4-0-2'];
    }
    // 2.获取当前用户的路由配置

    allRoutes.forEach(item => {
        if (checkedkeys.includes(item.key as string)) {
            //动态添加路由配置:vueRouter对象
            router.addRoute("mpage", { ...item });
        }
    })


    console.log("router.getRoutes()", router.getRoutes());

}


export const clearCurrentRoutes = () => {
    //1.获取当前用户的权限
    let checkedkeys: string[] = [];
    const grants = sessionStorage.getItem("grants");
    if (grants) {
        checkedkeys = JSON.parse(grants)
    }
    if (checkedkeys.length === 0) {
        checkedkeys = ['0-0', '0-0-0', '0-0-1', '0-0-2', '1-0', '1-0-0', '1-0-1', '1-0-2', '2-0', '2-0-0', '2-0-1', '2-0-2', '2-0-3', '3-0', '3-0-0', '3-0-1', '4-0', '4-0-0', '4-0-1', '4-0-2'];
    }
    // 2.删除当前用户的路由配置

    allRoutes.forEach(item => {
        if (checkedkeys.includes(item.key as string)) {
            //动态添加路由配置:vueRouter对象

            router.removeRoute(item.name as string);
        }
    })
}